package com.qire.manhua.dialog;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.qire.common.constant.CacheStorageKey;
import com.qire.common.support.base.BaseDialog;
import com.qire.common.utils.CacheStorageUtil;
import com.qire.manhua.R;
import com.qire.manhua.controller.ComicDownloadSelectorController;
import com.qire.manhua.databinding.DialogUnlockChapterOnComicDownloaderBinding;
import com.qire.manhua.model.viewModel.impl.ComicDownloadUnlockViewModel;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

/**
 * 在漫画下载器里的章节解锁对话框 - 只适用于 ComicDownloadSelectorActivity
 * @see com.qire.manhua.activity.ComicDownloadSelectorActivity
 */
public class UnlockChapterOnComicDownloaderDialog extends BaseDialog {

    private final boolean isFirst = CacheStorageUtil.queryBoolean(CacheStorageKey.COMIC_DOWNLOAD_UNLOCK_FIRST, true);

    private DialogUnlockChapterOnComicDownloaderBinding bindingView;

    public UnlockChapterOnComicDownloaderDialog(@NonNull FragmentActivity activity, ComicDownloadSelectorController controller, ComicDownloadUnlockViewModel unlockDialogViewModel){
        super(activity, R.style.FullDialog);
        LayoutInflater inflater = LayoutInflater.from(activity);
        bindingView = DialogUnlockChapterOnComicDownloaderBinding.inflate(inflater);
        bindingView.setComicDownloadSelectorController(controller);
        bindingView.setComicDownloadUnlockViewModel(unlockDialogViewModel);
        bindingView.setComicDownloadUnlockDialog(this);
    }

    private void setGravityBottom(){
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(bindingView.getRoot());
        setGravityBottom();
    }

    @Override
    public void show() {
        super.show();
        updateFirstShowState();
    }

    /**
     * 更新首次显示状态。引导层只在第一次显示，之后不在显示
     */
    public void updateFirstShowState() {
        if(isFirst) {
            CacheStorageUtil.save(CacheStorageKey.COMIC_DOWNLOAD_UNLOCK_FIRST, false);
        }
    }

    public int getGuideVisible() {
        return isFirst ? View.VISIBLE : View.GONE;
    }

}
